package com.miya.demo.common.util;

import java.io.*;

/**
 * 序列化工具类
 *
 * @author CaiXiaowei
 * @date 2022/09/05
 */
public class SerializeUtil {

    public static String serialize(Object obj) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream;
        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(obj);
        String string = byteArrayOutputStream.toString("ISO-8859-1");
        objectOutputStream.close();
        byteArrayOutputStream.close();
        return string;
    }

    public static <T> T serializeToObject(String str, T t) throws IOException, ClassNotFoundException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        T f = (T) objectInputStream.readObject();
        objectInputStream.close();
        byteArrayInputStream.close();
        return f;
    }
}